home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / dev / misc / ImageFXDevKit.lha / sdev / sas / libsrc / lib.asm next >
Encoding:
Assembly Source File  |  1993-04-07  |  11.5 KB  |  466 lines

  1. *************************************************************************
  2. * ImageFX Module Startup Code
  3. *
  4. * Based on the Commodore-supplied example library, if you didn't
  5. * notice.
  6. *
  7. *************************************************************************
  8.  
  9.  
  10. SAS_FLOAT    EQU    1    Enable SAS floating point init
  11. SAS_STACK    EQU    1    Enable SAS stack depth checking init
  12.  
  13.  
  14.            NOLIST
  15.  
  16.            INCLUDE "exec/types.i"
  17.            INCLUDE "exec/libraries.i"
  18.            INCLUDE "exec/lists.i"
  19.            INCLUDE "exec/alerts.i"
  20.            INCLUDE "exec/initializers.i"
  21.            INCLUDE "exec/resident.i"
  22.            INCLUDE "exec/tasks.i"
  23.            INCLUDE "libraries/dos.i"
  24.            INCLUDE "libraries/dosextens.i"
  25.  
  26.            INCLUDE "scan/mod.i"
  27.  
  28.  
  29. BHS         MACRO
  30.          bcc.\0  \1 ;\0 is the extension used on the macro (such as ".s")
  31.          ENDM
  32.  
  33. BLO         MACRO
  34.          bcs.\0  \1
  35.          ENDM
  36.  
  37.  
  38.            XREF       _AbsExecBase
  39.  
  40.            XREF    _LVOOpenLibrary
  41.            XREF    _LVOCloseLibrary
  42.            XREF    _LVOAlert
  43.            XREF    _LVOFreeMem
  44.            XREF    _LVORemove
  45.  
  46.            XREF    _LVOFindTask
  47.  
  48.            ; These must be defined in the C code somewhere...
  49.            XREF       _FuncTable            ; Function table array
  50.            XREF       _LibraryType              ; Type of module (NT_#?)
  51.            XREF       _LibraryID                ; Library ID string
  52.            XREF       _UserOpen                 ; Module-specific Open code
  53.            XREF       _UserClose                ; Module-specific Close code
  54.  
  55.         IFNE    SAS_FLOAT
  56.                 ; Used to allow floating point in modules.
  57.            XREF    ___fpinit
  58.            XREF    ___fpterm
  59.            ENDC    SAS_FLOAT
  60.  
  61.  
  62. ***********************************************************************
  63. *
  64. * Library data structures
  65. *
  66. ***********************************************************************
  67.  
  68.            LIST
  69.  
  70. ;----------------------------------------------------------------------
  71. ; The first executable location.  This should return an error
  72. ; in case someone tried to run you as a program (instead of
  73. ; loading you as a library).
  74.  
  75. Start:
  76.            moveq    #-1,d0
  77.            rts
  78.  
  79. ;-----------------------------------------------------------------------
  80. ; A romtag structure.  Both "exec" and "ramlib" look for
  81. ; this structure to discover magic constants about you
  82. ; (such as where to start running you from...).
  83. ;
  84.  
  85. initDDescrip:
  86.              DC.W    RTC_MATCHWORD   ; RT_MATCHWORD
  87.              DC.L    initDDescrip    ; RT_MATCHTAG
  88.              DC.L    EndCode         ; RT_ENDSKIP
  89.              DC.B    RTF_AUTOINIT    ; RT_FLAGS
  90.              DC.B    MOD_VERSION     ; RT_VERSION
  91.              DC.B    NT_LIBRARY      ; RT_TYPE
  92.              DC.B    0        ; RT_PRI
  93.              DC.L    0           ; RT_NAME
  94.              DC.L    _LibraryID      ; RT_IDSTRING
  95.              DC.L    Init            ; RT_INIT
  96.  
  97.  
  98.         ; force word allignment
  99.            DS.W    0
  100.  
  101.  
  102. ;----------------------------------------------------------------------
  103. ; The romtag specified that we were "RTF_AUTOINIT".  This means
  104. ; that the RT_INIT structure member points to one of these
  105. ; tables below.  If the AUTOINIT bit was not set then RT_INIT
  106. ; would point to a routine to run.
  107.  
  108. Init:
  109.            DC.L    xmodbase_SIZEOF   ; size of library base data space
  110.            DC.L    _FuncTable        ; pointer to function initializers (external)
  111.            DC.L    dataTable         ; pointer to data initializers
  112.            DC.L    initRoutine       ; routine to run
  113.  
  114.  
  115. ;----------------------------------------------------------------------
  116. ; The data table initializes static data structures.
  117. ; The format is specified in exec/InitStruct routine's
  118. ; manual pages.  The INITBYTE/INITWORD/INITLONG routines
  119. ; are in the file "exec/initializers.i".  The first argument
  120. ; is the offset from the library base for this byte/word/long.
  121. ; The second argument is the value to put in that cell.
  122. ; The table is null terminated
  123.  
  124. dataTable:
  125.            INITBYTE       LIB_FLAGS,LIBF_SUMUSED!LIBF_CHANGED
  126.            INITWORD       LIB_VERSION,MOD_VERSION
  127.            INITWORD       LIB_REVISION,MOD_REVISION
  128.            INITLONG       LIB_IDSTRING,_LibraryID
  129.            DC.L        0
  130.  
  131.  
  132. ;----------------------------------------------------------------------
  133. ; This routine gets called after the library has been allocated.
  134. ; The library pointer is in D0.  The segment list is in A0.
  135. ; If it returns non-zero then the library will be linked into
  136. ; the library list.
  137.  
  138. initRoutine:
  139.         ;------ get the library pointer into a convenient A register
  140.         move.l    a5,-(sp)
  141.         move.l    d0,a5
  142.  
  143.         ;------ save a pointer to exec
  144.         move.l    a6,mb_SysLib(a5)
  145.         move.l    a6,_SysBase
  146.  
  147.         ;------ save a pointer to our loaded code
  148.         move.l    a0,mb_Segment(a5)
  149.  
  150.         IFNE    SAS_STACK
  151.  
  152.         ;------ initialize SAS stack depth checking
  153.         suba.l    a1,a1
  154.         jsr    _LVOFindTask(a6)
  155.         move.l    d0,a1
  156.         move.l    pr_CLI(a1),d1
  157.         beq.s    11$
  158.         lsl.l    #2,d1
  159.         move.l    d1,a0
  160.         move.l    cli_DefaultStack(a0),d1
  161.         lsl.l    #2,d1
  162.         bra.s    21$
  163. 11$
  164.         move.l    a7,d1
  165.         sub.l    TC_SPLOWER(a1),d1
  166. 21$
  167.         move.l    a7,d0
  168.         sub.l    d1,d0
  169.         add.l    #128,d0
  170.         move.l    d0,___base
  171.  
  172.         ENDC    SAS_STACK
  173.  
  174.         ;------ open the dos library
  175.         lea    dosName(pc),a1
  176.         moveq    #0,d0
  177.         jsr    _LVOOpenLibrary(a6)
  178.  
  179.         move.l    d0,_DOSBase
  180.         bne.s    1$
  181.  
  182.         ;------ can't open the dos!  what gives
  183.         ALERT    AG_OpenLib!AO_DOSLib
  184.  
  185. 1$:
  186.         ;------ initialize floating point libraries
  187.         IFNE    SAS_FLOAT
  188.         jsr      ___fpinit(PC)
  189.                 ENDC    SAS_FLOAT
  190.  
  191.         move.l    a5,d0            ; restore libbase
  192. OUT:
  193.         move.l    (sp)+,a5
  194.         rts
  195.  
  196.         IFNE    SAS_FLOAT
  197.  
  198. ;--------------------------------------------------------------------------
  199. ; We need this because of SAS lcmieee.lib floating point initialization.
  200. ; If it fails, it calls XCEXIT(), which we are trapping here and turning
  201. ; into just a standard failure return.  Not nice, but it seems to work.
  202. ;
  203.         xdef    __XCEXIT
  204. __XCEXIT:
  205.         moveq    #0,d0
  206.         addq.l    #8,sp
  207.         bra.s    OUT
  208.  
  209.                 ENDC    SAS_FLOAT
  210.  
  211.  
  212. ************************************************************************
  213. *
  214. * Here begins the system interface commands.  When the user calls
  215. * OpenLibrary/CloseLibrary/RemoveLibrary, this eventually gets translated
  216. * into a call to the following routines (Open/Close/Expunge).  Exec
  217. * has already put our library pointer in A6 for us.  Exec has turned
  218. * off task switching while in these routines (via Forbid/Permit), so
  219. * we should not take too long in them.
  220. *
  221. ************************************************************************
  222.  
  223.            XDEF       _LibOpen
  224.            XDEF       _LibClose
  225.            XDEF       _LibExpunge
  226.            XDEF       _LibNull
  227.  
  228. ;--------------------------------------------------------------------
  229. ; Open returns the library pointer in d0 if the open
  230. ; was successful.  If the open failed then null is returned.
  231. ; It might fail if we allocated memory on each open, or
  232. ; if only open application could have the library open
  233. ; at a time...
  234.  
  235. _LibOpen:    ; ( libptr:a6, version:d0 )
  236.  
  237.         move.l    a4,-(sp)    ; Save opener's A4...
  238.  
  239.                 ; Set type of module.
  240.            move.b  _LibraryType,LN_TYPE(a6)
  241.  
  242.         ; Set module's global pointers...
  243.         move.l    a6,_ModuleBase
  244.         move.l    mb_ScanBase(a6),a0
  245.         move.l    a0,_ScanBase
  246.         move.l    sb_IntuitionBase(a0),_IntuitionBase
  247.         move.l    sb_GfxBase(a0),_GfxBase
  248.  
  249.            ; Call module's 'C' open code
  250.            move.l a6,-(sp)
  251.            bsr    _UserOpen
  252.            move.l (sp)+,a6
  253.            tst.l  d0
  254.            bne.s  1$
  255.  
  256.            ; User open failed, abort open
  257.            move.l (sp)+,a4        ; Restore A4!
  258.            moveq  #0,d0        ; Fail code
  259.            rts
  260.  
  261. 1$:
  262.            ; mark us as having another opener
  263.            addq.w   #1,LIB_OPENCNT(a6)
  264.  
  265.            ; prevent delayed expunges
  266. ;           bclr   #LIBB_DELEXP,LIB_FLAGS(a6)
  267.  
  268.            move.l   (sp)+,a4    ; Restore opener's A4
  269.  
  270.            move.l   a6,d0        ; Success
  271.            rts
  272.  
  273.  
  274. ;----------------------------------------------------------------
  275. ; There are two different things that might be returned from
  276. ; the Close routine.  If the library is no longer open and
  277. ; there is a delayed expunge then Close should return the
  278. ; segment list (as given to Init).  Otherwise close should
  279. ; return NULL.
  280.  
  281. _LibClose:    ; ( libptr:a6 )
  282.  
  283.            move.l  a4,-(sp)        ; Save opener's A4...
  284.  
  285.            ;------ Call 'C' close code
  286.            move.l  a6,-(sp)
  287.            bsr     _UserClose
  288.            move.l  (sp)+,a6
  289.  
  290.            ;------ set the return value
  291.            moveq   #0,d0
  292.  
  293.            ;------ mark us as having one fewer openers
  294.            subq.w   #1,LIB_OPENCNT(a6)
  295.  
  296.            ;------ see if there is anyone left with us open
  297.            bne.s   1$
  298.  
  299.            ;------ We expunge the library no matter what on the last Close
  300. ;           ;------ see if we have a delayed expunge pending
  301. ;          btst   #LIBB_DELEXP,LIB_FLAGS(a6)
  302. ;          beq.s   1$
  303.  
  304.            ;------ do the expunge
  305.            bsr   _LibExpunge
  306. 1$:
  307.            move.l (sp)+,a4            ; Restore opener's A4...
  308.            rts
  309.  
  310.  
  311. ;---------------------------------------------------------------
  312. ; There are two different things that might be returned from
  313. ; the Expunge routine.  If the library is no longer open
  314. ; then Expunge should return the segment list (as given to
  315. ; Init).  Otherwise Expunge should set the delayed expunge
  316. ; flag and return NULL.
  317. ;
  318. ; One other important note: because Expunge is called from
  319. ; the memory allocator, it may NEVER Wait() or otherwise
  320. ; take long time to complete.
  321.  
  322. _LibExpunge:    ; ( libptr: a6 )
  323.  
  324.         movem.l    d2/a5/a6,-(sp)
  325.         move.l    a6,a5
  326.         move.l    mb_SysLib(a5),a6
  327.  
  328.         ;------ see if anyone has us open
  329.         tst.w    LIB_OPENCNT(a5)
  330.         beq.s    1$
  331.  
  332.         ;------ it is still open.  set the delayed expunge flag
  333.         bset    #LIBB_DELEXP,LIB_FLAGS(a5)
  334.         moveq    #0,d0
  335.         bra.s    Expunge_End
  336.  
  337. 1$:
  338.         IFNE    SAS_FLOAT
  339.         jsr    ___fpterm(PC)
  340.         ENDC    SAS_FLOAT
  341.  
  342.         ;------ go ahead and get rid of us.  Store our seglist in d2
  343.         move.l    mb_Segment(a5),d2
  344.  
  345.         ; no need to remove library - we were never added
  346.         ; to public library list!
  347.  
  348.         ;------ close the dos library
  349.         move.l    _DOSBase,a1
  350.         jsr    _LVOCloseLibrary(a6)
  351.  
  352.         ;------ free our memory
  353.         moveq    #0,d0
  354.         move.l    a5,a1
  355.         move.w    LIB_NEGSIZE(a5),d0
  356.  
  357.         sub.l    d0,a1
  358.         add.w    LIB_POSSIZE(a5),d0
  359.  
  360.         jsr    _LVOFreeMem(a6)
  361.  
  362.         ;------ set up our return value, the segment
  363.         move.l    d2,d0
  364.  
  365. Expunge_End:
  366.         movem.l    (sp)+,d2/a5/a6
  367.         rts
  368.  
  369.  
  370. ;----------------------------------------------------------
  371. ; Reserved library vector should just return 0.
  372. ;
  373.  
  374. _LibNull:
  375.         moveq    #0,d0
  376.         rts
  377.  
  378. ;----------------------------------------------------------
  379. ; If language text has been located and loaded, use the
  380. ; string associated with the given index.  Otherwise use
  381. ; the default string passed in.
  382. ;
  383. ; May be used by a module to provide the ability to alter
  384. ; the module's text strings, for localization purposes.
  385. ;
  386. ; char *GetStr(LONG index, char *default);
  387. ;
  388.  
  389.         XDEF    _GetStr
  390. _GetStr:
  391.         move.l    _ModuleBase,a0
  392.         tst.l    mb_Text(a0)
  393.         beq.s    1$
  394.         move.l    mb_Text(a0),a0
  395.         move.l    4(sp),d0
  396.         add.l    d0,d0
  397.         add.l    d0,d0
  398.         move.l    0(a0,d0.l),d0
  399.         bra.s    2$
  400. 1$        move.l    8(sp),d0
  401. 2$        rts
  402.  
  403.  
  404. **********************************************************************
  405. *
  406. * Data section
  407. *
  408. **********************************************************************
  409.  
  410.            XDEF       _SysBase
  411.            XDEF       _DOSBase
  412.                 XDEF    _IntuitionBase
  413.                 XDEF    _GfxBase
  414.                 XDEF    _ModuleBase
  415.                 XDEF    _ScanBase
  416.  
  417.  
  418. dosName:    DC.B    'dos.library',0
  419.            CNOP    0,2
  420.  
  421.  
  422.         SECTION __MERGED,DATA
  423.  
  424. _SysBase:    DC.L    0
  425. _DOSBase:    DC.L    0
  426.  
  427. _IntuitionBase  DC.L    0
  428. _GfxBase        DC.L    0
  429. _ModuleBase     DC.L    0
  430. _ScanBase       DC.L    0
  431.  
  432.  
  433. ; For SAS stack depth checking...
  434.         IFNE    SAS_STACK
  435.         XDEF    ___base
  436. ___base:    DC.L    0
  437.         ENDC    SAS_STACK
  438.  
  439. ; These are used for SAS floating point...
  440.         IFNE    SAS_FLOAT
  441.         XDEF    __FPERR,__SIGFPE
  442. __FPERR        DC.L    0
  443. __SIGFPE    DC.L    0
  444.         ENDC    SAS_FLOAT
  445.  
  446. ;--------------------------------------------------------------------
  447. ; This stuff was added for modules to compile under SAS6.2...
  448.  
  449.         XDEF    __OSERR
  450. __OSERR:    DC.L    0        ; OS error codes (SAS6.2)
  451.         XDEF    __ProgramName
  452. __ProgramName    DC.L    0        ; Program Name??? (SAS6.2)
  453.  
  454.  
  455.  
  456. ; EndCode is a marker that show the end of your code.
  457. ; Make sure it does not span sections nor is before the
  458. ; rom tag in memory!  It is ok to put it right after
  459. ; the rom tag -- that way you are always safe.  I put
  460. ; it here because it happens to be the "right" thing
  461. ; to do, and I know that it is safe in this case.
  462.  
  463. EndCode:
  464.         END
  465.  
  466.